package com.cooper.java.scraper;

import org.dom4j.Element;

/**
 * Created with IntelliJ IDEA.
 * User: cooperlee
 * Date: 11/16/12
 * Time: 3:25 PM
 */
public class DefineTag {


    public final static String REG_EXP = "RegExp";
    public final static String EXPRESSION_TAG = "expression";

    private static RegExp getRegExp(Scraper scraper, Element regElement) {
        RegExp result = new RegExp(scraper, regElement.attribute("input").getValue(), regElement.attribute("output").getValue(), regElement.attribute("dest").getValue(), regElement.attribute("conditional") == null ? "" : regElement.attribute("conditional").getValue());
        Element expElement = regElement.element(EXPRESSION_TAG);
        result.exp = new Exp(expElement.attribute("repeat") == null ? "" : expElement.attribute("repeat").getValue(), expElement.attribute("noclean") == null ? "" : expElement.attribute("noclean").getValue(), expElement.attribute("trim") == null ? "" : expElement.attribute("trim").getValue(), expElement.attribute("clear") == null ? "" : expElement.attribute("clear").getValue());
        result.exp.rule = expElement.getText();

        for (java.util.Iterator i = regElement.elementIterator(REG_EXP); i.hasNext(); ) {
            result.addSubRegExp(getRegExp(scraper, (Element) i.next()));
        }
        return result;
    }

    public static DefineTag defineTagFactory(Scraper scraper,
                                             Element xmlRoot, String tagName) {

        Element tagElement = xmlRoot.element(tagName);
        if (tagElement == null) {
            return null;
        }
        DefineTag result = new DefineTag(scraper, tagElement.attribute("dest").getValue(), tagElement.attribute("SearchStringEncoding") == null ? "" : tagElement.attribute("SearchStringEncoding").getValue(), tagElement.attribute("clearbuffers") == null ? "" : tagElement.attribute("clearbuffers").getValue());

        result.regExp = getRegExp(scraper, tagElement.element(REG_EXP));
        return result;

        /*
    RegExp regExp = null;
    try {
        int eventType = xml.getEventType();
        boolean found = false;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (tagName.equals(xml.getName())) {
                    found = true;
                    result = new DefineTag(scraper, xml);
                } else if (found && REG_EXP.equals(xml.getName())) {
                    if (regExp == null) {
                        regExp = new RegExp(scraper, xml);
                        result.regExp = regExp;
                    } else {
                        regExp = regExp.addSubRegExp(new RegExp(scraper,
                                xml));
                    }
                } else if (found && EXPRESSION_TAG.equals(xml.getName())) {
                    regExp.exp = new Exp(xml);
                }
            } else if (eventType == XmlPullParser.TEXT) {
                if (found && (regExp.exp != null)) {
                    regExp.exp.rule = xml.getText();
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                if (found && tagName.equals(xml.getName())) {
                    break;
                } else if (found && REG_EXP.equals(xml.getName())
                        && (regExp.getParentRegExp() != null)) {
                    regExp = regExp.getParentRegExp();
                }
            }
            eventType = xml.next();
        }
        return result;
    } finally {
        xml.close();
    }    */
    }

    private Integer dest;
    private String encodeType;
    private boolean clearbuffers;
    private RegExp regExp;
    private Scraper scraper;

    /*

    public DefineTag(Scraper scraper, XmlPullParser xml) {
        this.scraper = scraper;
        String destStr = xml.getAttributeValue(null, "dest");
        dest = Integer.parseInt(destStr);
        encodeType = xml.getAttributeValue(null, "SearchStringEncoding");
        clearbuffers = Scraper.parseBool(
                xml.getAttributeValue(null, "clearbuffers"), true);
    }     */

    public DefineTag(Scraper scraper, String destStr, String encodeType, String clearbuffers) {
        this.scraper = scraper;
        dest = Integer.parseInt(destStr);
        this.encodeType = encodeType;
        this.clearbuffers = Scraper.parseBool(clearbuffers, true);
    }

    public String matchRegExp() throws DownloadException {
        regExp.matchExp();
        String result = scraper.getBuffer()[dest];
        if (clearbuffers) {
            for (int i = 0; i < scraper.getBuffer().length; i++) {
                scraper.getBuffer()[i] = "";
            }
        }
        return result;
    }

    public String getEncodeType() {
        return encodeType;
    }

}
